package ui;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.util.Random;

public class GameJFrame extends JFrame implements KeyListener,ActionListener{
    JMenu replayJMenu=new JMenu("重新游戏");
    JMenuItem reLoginItem=new JMenuItem("重新登陆");
    JMenuItem closeItem=new JMenuItem("关闭游戏");
    JMenuItem accountItem=new JMenuItem("我的微信");

    JMenuItem moneyItem=new JMenuItem("欢迎打赏");
    JMenuItem animals=new JMenuItem("动物");
    JMenuItem girls=new JMenuItem("美女");
    JMenuItem motion=new JMenuItem("运动");
    int[][] data=new int[4][4];//定义在尘缘位置，大家都可以去用
    //用于记录空白图片的位置
    int num=1;
    int x=0;
    int y=0;
    int step=0;
    String path="D:\\IJ\\javaCode\\puzzleGame\\engineering-practice-\\image\\animal\\animal1\\";
    public GameJFrame(){
        //初始化界面
        initJFrame();
        //初始化菜单
        initJmenmbar();
        //创建二维数组勇用于管理数据
        ininData();
        //初始化图片
        initImage();
        //显示窗体
        this.setVisible(true);
    }
    //打乱图片
    private void ininData() {
        int[] arr={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
        Random r =new Random();
        for(int i=0;i<arr.length;i++)
        {
            int num=r.nextInt(arr.length);
            int temp=arr[i];
            arr[i]=arr[num];
            arr[num]=temp;
        }
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){

                if(arr[4*i+j]==0){
                    x=i;
                    y=j;

                }
                data[i][j]=arr[4*i+j];
            }
        }
    }
    //初始化图片
    private void initImage() {
        this.getContentPane().removeAll();//清屏
        JLabel stepcount=new JLabel("步数:"+step);
        stepcount.setBounds(50,30,100,20);
        this.getContentPane().add(stepcount);
        //因为先加载的图片会在上方
        if (this.Judgethevictory()){
            this.showwin();
        }
        //System.out.println();
        step++;//可以添加在 上下左右的地方

        for(int j=0;j<4;j++){
            for(int i=0;i<4;i++){
                /*创建图片对象ImageIcon的对象*/
                //创建一个Jlabel的对象(管理容器)
                JLabel jLabel=new JLabel(new ImageIcon(path+(this.data[j][i])+".jpg"));
                //指定图片位置
                jLabel.setBounds(105*i+83,105*j+134,105,105);
                //把管理容器添加到界面当中
                //this.add(jLabel);
                //0:图片突起
                //1:图片凹陷
                jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));//设置边框
                //最后一步
                this.getContentPane().add(jLabel);//把图片放到管理他的容器里面
            }
        }
        /*创建图片对象ImageIcon的对象*/
        //添加背景
        //ImageIcon icon=//背景图片暂时不用变
        JLabel background=new JLabel(new ImageIcon("D:\\IJ\\javaCode\\puzzleGame\\engineering-practice-\\image\\background.png"));
        background.setBounds(40,40,508,560);
        this.getContentPane().add(background);

        this.getContentPane().repaint();

    }
    //初始化菜单
    private void initJmenmbar() {
        JMenuBar jMenuBar=new JMenuBar();//菜单
        JMenu functionJmenu=new JMenu("功能");//功能选项卡
        JMenu aboutJmenu=new JMenu("关于我");//关于选项卡

        replayJMenu.add(animals);
        replayJMenu.add(girls);
        replayJMenu.add(motion);

        functionJmenu.add(replayJMenu);
        functionJmenu.add(reLoginItem);
        functionJmenu.add(closeItem);
        aboutJmenu.add(accountItem);
        aboutJmenu.add(moneyItem);

        jMenuBar.add(functionJmenu);
        jMenuBar.add(aboutJmenu);

        //绑定事件
        //replayJMenu.addActionListener(this);
        animals.addActionListener(this);
        girls.addActionListener(this);
        motion.addActionListener(this);
        reLoginItem.addActionListener(this);
        closeItem.addActionListener(this);
        accountItem.addActionListener(this);
        moneyItem.addActionListener(this);

        this.setJMenuBar(jMenuBar);
    }
    //初始化界面
    private void initJFrame() {
        //设置页面大小
        this.setSize(603,680);
        //设置标题
        this.setTitle("单机拼图 v1.0");
        //设置页面置顶
        this.setAlwaysOnTop(true);
        //设置位置居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        /* 3 代表关闭一个页面以后就自动关闭程序，该单机游戏每一次只允许一个页面存在
         * */
        this.setDefaultCloseOperation(3);
        //取消默认的居中放置，采用指定的(x,y)坐标进行放置
        this.setLayout(null);

        this.addKeyListener(this);//给界面添加事件
    }
    //展示胜利
    private void showwin(){

        ImageIcon icon=new ImageIcon("D:\\IJ\\javaCode\\puzzleGame\\engineering-practice-\\image\\win.png");
        JLabel win=new JLabel(icon);
        win.setBounds(203,283,197,73);
        this.getContentPane().add(win);
        //this.getContentPane().repaint();

    }
    //判断胜利
    private boolean Judgethevictory(){
        int[][] data1={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0}};
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data.length; j++) {
                if(data[i][j]!=data1[i][j]){
                    return false;
                }
            }
        }
        return true;
    }
    @Override
    public void keyTyped(KeyEvent keyEvent) {

    }
    @Override
    public void keyPressed(KeyEvent e) {
        int code=e.getKeyCode();
        if(code==65){//查看原图
            this.getContentPane().removeAll();
            JLabel all=new JLabel(new ImageIcon(path+"all.jpg"));
            all.setBounds(83,134,420,420);
            this.getContentPane().add(all);
            ImageIcon icon=new ImageIcon("D:\\IJ\\javaCode\\puzzleGame\\engineering-practice-\\image\\background.png");
            JLabel background=new JLabel(icon);
            background.setBounds(40,40,508,560);
            this.getContentPane().add(background);
            this.getContentPane().repaint();
        }


    }
    @Override
    public void keyReleased(KeyEvent e) {
        if(Judgethevictory()){
            return ;//游戏胜利不允许在移动
        }
        int code=e.getKeyCode();//获得按键
        if(code==37){
            if(y<3){
                System.out.println("向左移动");
                int temp=data[x][y];
                data[x][y]=data[x][y+1];
                data[x][y+1]=0;
                y+=1;
                this.initImage();//
            }else {
                System.out.println("移动失败");
            }
        } else if (code==38) {
            if(x<3){
                System.out.println("向上移动");
                int temp=data[x][y];
                data[x][y]=data[x+1][y];
                data[x+1][y]=0;
                x+=1;
                this.initImage();

            }else {
                System.out.println("移动失败");
            }

        }else if (code==39) {
            if(y>0){
                System.out.println("向右移动");
                int temp=data[x][y-1];
                data[x][y]=data[x][y-1];
                data[x][y-1]=0;
                y-=1;
                this.initImage();
            }else {
                System.out.println("移动失败");
            }
        }else if (code==40) {
            if(x>0){
                System.out.println("向下移动");
                int temp=data[x][y];
                data[x][y]=data[x-1][y];
                data[x-1][y]=0;
                x-=1;
                this.initImage();
            }else {
                System.out.println("移动失败");
            }
        }else if(code==65) {
            this.initImage();
        } else if (code==87) {
            for(int i=0;i< data.length;i++){
                for (int j = 0; j < data.length; j++) {
                    data[i][j]=4*i+j+1;
                }
            }
            x=3;
            y=3;
            data[3][3]=0;
            this.initImage();
            System.out.println(Judgethevictory());
        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        Object source=e.getSource();
        if (source==closeItem) {
            System.out.println("关闭游戏");
            System.exit(1);
        }
        else if(source==animals){
            System.out.println("重新游戏");
            this.ininData();
            step=0;
            Random r =new Random();
            int num1=num;
            while(true){
                num=r.nextInt(7)+1;
                if (num1!=num)
                    break;
            }
            path="D:\\IJ\\javaCode\\puzzleGame\\engineering-practice-\\image\\animal\\animal"+num+"\\";
            this.initImage();
        }
        else if(source==girls){
            System.out.println("重新游戏");
            this.ininData();
            step=0;
            Random r =new Random();
            int num1=num;
            while(true){
                num=r.nextInt(12)+1;
                if (num1!=num)
                    break;
            }
            path="D:\\IJ\\javaCode\\puzzleGame\\engineering-practice-\\image\\girl\\girl"+num+"\\";
            this.initImage();
        }
        else if(source==motion){
            System.out.println("重新游戏");
            this.ininData();
            step=0;
            Random r =new Random();
            int num1=num;
            while(true){
                num=r.nextInt(9)+1;
                if (num1!=num)
                    break;
            }
            path="D:\\IJ\\javaCode\\puzzleGame\\engineering-practice-\\image\\sport\\sport"+num+"\\";
            this.initImage();
        }
        else if (source==accountItem) {
            System.out.println("关于我");
            JDialog jDialog=new JDialog();
            JLabel jLabel=new JLabel(new ImageIcon("D:\\IJ\\javaCode\\puzzleGame\\engineering-practice-\\image\\myWeChat.png"));
            jLabel.setBounds(0,0,450,613);
            jDialog.getContentPane().add(jLabel);
            jDialog.setSize(450,613);
            jDialog.setAlwaysOnTop(true);
            jDialog.setLocationRelativeTo(null);
            jDialog.setModal(true);
            jDialog.setVisible(true);
        }else if (source==moneyItem) {
            System.out.println("关于我");
            JDialog jDialog=new JDialog();
            JLabel jLabel=new JLabel(new ImageIcon("D:\\IJ\\javaCode\\puzzleGame\\engineering-practice-\\image\\moeny.png"));
            jLabel.setBounds(0,0,518,706);
            jDialog.getContentPane().add(jLabel);
            jDialog.setSize(518,706);
            jDialog.setAlwaysOnTop(true);
            jDialog.setLocationRelativeTo(null);
            jDialog.setModal(true);
            jDialog.setVisible(true);
        }
        else if (source==reLoginItem) {
            System.out.println("重新登陆");
            this.setVisible(false);//关闭当前页面
            new LoginJFrame();//打开登陆页面
        }
    }
}
